본 게시글의 원문은 Yann Debray의 MATLAB with Python Book 입니다. 해당 책은 MIT 라이센스를 따르기 때문에 개인적으로 번역하여 재배포 합니다. 본 포스팅에는 추후 유료 수익을 위한 광고가 부착될 수도 있습니다.

MIT License

Copyright (c) 2023 Yann Debray

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

본 게시물에서 활용된 소스 코드들은 모두 Yann Debray의 GitHub Repo에서 확인할 수 있습니다.

4.3. MATLAB에서 Python 호출의 기본 구문

MATLAB에서 Python 함수를 호출하는 모든 구문은 기본적으로 동일한 구문을 가지고 있습니다:

제가 일반적으로 처음에 제시하는 기본 예제는 일반적으로 math 모듈의 제곱근 함수를 호출하는 것입니다. MATLAB에서 Python에서 수학 함수를 호출하는 것은 그다지 의미가 없지만, 결과를 MATLAB에서 기대한 것과 직접 비교하는 것은 쉽습니다:

MATLAB 명령창에서:

>> py.math.sqrt(42)

MATLAB 라이브 스크립트에서:

py.math.sqrt(42)
ans = 6.4807

우리는 MATLAB 내에서 Python 데이터 구조를 생성할 수 있습니다:

py.list([1,2,3])
ans = 
  Python list with values:

    [1.0, 2.0, 3.0]

    Use string, double or cell function to convert to a MATLAB array.

py.list({1,2,'a','b'})
ans = 
  Python list with values:

    [1.0, 2.0, 'a', 'b']

    Use string, double or cell function to convert to a MATLAB array.

s = struct('a', 1, 'b', 2)
s = 
    a: 1
    b: 2

d = py.dict(s)
d = 
  Python dict with no properties.

    {'a': 1.0, 'b': 2.0}

또한 MATLAB 측에서 이러한 데이터 구조에 대한 메서드를 실행할 수 있습니다:

methods(d)
Methods for class py.dict:

char        copy        eq          get         items       le          ne          popitem     struct      values      
clear       dict        ge          gt          keys        lt          pop         setdefault  update      

Static methods:

fromkeys    

Methods of py.dict inherited from handle.
d.get('a')
ans = 1